/* Sean Waitkus 362 Project 1.1.c 2-10-2014  This program is labled 1.1 because of issues with SVN.
This program takes in a text file containing html tags and outputs the text with the html tags stripped.
It than counts the number of removed tags, number of original characters and the number of characters written to the output file.
Finally it prints to the console window the file names, number of removed tags, the number of original characters and written characters and 
computes the size difference between the input and output file in percentile form.
*/
//For my personal use I comment important braces to ease detection of missing braces. I have left these comments in place.
#include "stdafx.h"
#include <stdio.h>

void main(){
	
	FILE *ifp, *ofp;//input/output file pointers
	int c, c2, c3; //variables used to store and compare input characters, c2 is used only to check for a </p> tag c1 is no longer used
	int n = 0;//used to halt the console window with scanf
	int count = 0;//counter for total characters in file
	int discardTag = 0;	//counter to store number of discarded tags
	int br = 0; //counter for <br /> this was used as a debugging statement
	int p = 0;//counter for <p> this was used as a debugging statement
	double charDiff;//variable to store file size difference
	int putCount = 0;//counter for number of outputted characters
	ifp = fopen("prog1in2.txt", "r");
	if(ifp == NULL){	
	/*we have not covered error catching as of yet
	but this is here simply as a reminder to myself to do so
	especially with input errors*/
	}
	ofp = fopen("prog1in2out.txt", "w");
	
	do{
		c = getc(ifp);//gets the first/next character in the file
		count ++;
		
		//compares the current character to '<' if its found starts a while loop
		if(c == '<'){
			discardTag ++;
			//loops until it reaches the end of the tag
			while( c != '>'){
				count ++;
				c = getc(ifp);
				
				/*if the first character in the tag is '/' assigns c to c2*/
				if( c == '/' ){
				 c2 = c;

				}
				
				/*if 'b' is found sets the next character to c3
				and compares them to determine if it is a <br /> tag 
				if true outputs a null line increments the <br /> counter/*/
				else if(c == 'b' ){
					c3 = getc(ifp);
					if( c == 'b' && c3 == 'r'){
						putc( '\n' , ofp);
						putCount ++;
						br ++;
					
					}
				}//end <br /> if statement
			
				/*checks if the current character is a 'p' and that c2 is not '/'
				to determine if the tag is <p> if true outputs two null lines 
				and counts the number of <p> tags*/
				else if ( c == 'p' && c2 != '/' ){
					p ++;
					putc( '\n' ,ofp);
					putc( '\n', ofp);
					putCount += 2;
					
				}//end <p> if statement		
				
			}//end while
			
		}//end of tag detection logic

		c2 = 0;//resets c2 for the next loop iteration
		if(c == '\n'){ //counts '\n' lines
			count ++;
			
		}
		/*checks if the current character is not '>' or '\n' or EOF
		if true outputs the current character*/
		if( c != '>' ){
			if(c != '\n'){
				if( c != EOF){
					putc( c , ofp);
					putCount++;
				}
			}
		}
	
		
	}while(c != EOF);//ends the file read
	fclose(ifp);

	/*computes the difference in file sizes into a two decimal percentile
	and prints the file names, number of tags, characters and the percent of decrease*/
	charDiff = (putCount/((double)count)) * 100 ;
	printf("%c%s%s",'\n',"Input File Name:   ", "prog1in2.txt");
	printf("%c%s%s",'\n',"Output File Name:  ", "prog1in2out.txt");
	printf("%c%s%d",'\n',"Number Of Tags     ", discardTag);
	printf("%c%s%d",'\n',"Input File Size:   ", count);
	printf("%c%s%d",'\n',"Output File Size:  ", putCount);
	printf("%c%s%.2f%c",'\n',"Percent Decrease:  ", charDiff, '%' );
	scanf("%d", &n);
	

}//end main
